package com.hmdp.listener;
import com.hmdp.entity.VoucherOrder;
import com.hmdp.service.impl.VoucherOrderServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.support.Acknowledgment;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class KafkaConsumer {
    @Autowired
    private VoucherOrderServiceImpl voucherOrderService;

    @KafkaListener(topics = "order-topic", groupId = "order-group")
    public void listen(ConsumerRecord<String, VoucherOrder> record, Acknowledgment ack) {
        try {
            log.info("消费消息: {}", record.value());
            voucherOrderService.save(record.value());
            ack.acknowledge();
        } catch (Exception e) {
            // 可以记录异常，重试等
            log.error("消费失败: {}", e.getMessage());
        }
    }
}
